package com.atguigu.gmall.order.config;


import com.atguigu.gmall.common.constant.MqConst;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 1：为了取消订单
 *  创建延迟交换机
 *  创建普通队列
 *  准备RoutingKey  进行绑定
 */
@Configuration
public class OrderMqConfig {

    //创建延迟交换机
    @Bean
    public Exchange cancelOrderExchange(){
        Map<String,Object> arguments = new HashMap<>();
        arguments.put("x-delayed-type","direct");
        //自定义交换机
        return new CustomExchange(MqConst.EXCHANGE_DIRECT_ORDER_CANCEL,"x-delayed-message",
                true,false,arguments);
    }
    //创建队列  取消订单
    @Bean
    public Queue cancelOrderQueue(){
        return QueueBuilder.durable(MqConst.QUEUE_ORDER_CANCEL).build();
    }
    //绑定
    @Bean
    public Binding bindingCancelOrderQueueToCancelOrderExchange(){
        return BindingBuilder.bind(cancelOrderQueue()).to(cancelOrderExchange())
                .with(MqConst.ROUTING_ORDER_CANCEL).noargs();
    }
    //创建队列  更新支付信息及关闭 支付宝
    @Bean
    public Queue closePaymentQueue(){
        return QueueBuilder.durable(MqConst.QUEUE_PAYMENT_CLOSE).build();
    }
    //绑定
    @Bean
    public Binding bindingClosePaymentQueueToCancelOrderExchange(){
        return BindingBuilder.bind(closePaymentQueue()).to(cancelOrderExchange())
                .with(MqConst.ROUTING_ORDER_CANCEL).noargs();
    }




    ////////////////////////////更新订单///////////////////////////////////////////////////
    //创建交换机
    @Bean
    public Exchange updateOrderExchange(){
        return ExchangeBuilder.directExchange(MqConst.EXCHANGE_DIRECT_PAYMENT_PAY).build();
    }
    //创建队列
    @Bean
    public Queue updateOrderQueue(){
        return QueueBuilder.durable(MqConst.QUEUE_PAYMENT_PAY).build();
    }
    //绑定
    @Bean
    public Binding bindingUpdateOrderQueueToUpdateOrderExchange(){
        return BindingBuilder.bind(updateOrderQueue()).to(updateOrderExchange())
                .with(MqConst.ROUTING_PAYMENT_PAY).noargs();
    }
}
